﻿using CSRedis;

using KSOAdmin.Core.AppsettingHelper;

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using KSOAdmin.Core.CacheHelper.Interface;
using KSOAdmin.Core.Other;

namespace KSOAdmin.Core.CacheHelper.Service
{
    public class RedisCacheService : ICacheService
    {
        public RedisCacheService()
        {
            var csredis = new CSRedisClient(Appsettings.app("RedisConfig"));
            RedisHelper.Initialization(csredis);
        }

        /// <summary>
        /// 验证缓存项是否存在
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public bool Exists(string key, CacheDataType dataType)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            key = this.RedisKey(key, dataType);
            return RedisHelper.Exists(key);
        }

        public void LPush(string key, string val, CacheDataType dataType)
{
            key = this.RedisKey(key, dataType);
            RedisHelper.LPush(key, val);
        }

        public void RPush(string key, string val, CacheDataType dataType)
        {
            key = this.RedisKey(key, dataType);
            RedisHelper.RPush(key, val);
        }

        public T ListDequeue<T>(string key, CacheDataType dataType) where T : class
        {
            key = this.RedisKey(key, dataType);
            string value = RedisHelper.RPop(key);
            if (string.IsNullOrEmpty(value))
                return null;
            return JsonConvert.DeserializeObject<T>(value);
        }
        public object ListDequeue(string key, CacheDataType dataType)
        {
            key = this.RedisKey(key, dataType);
            string value = RedisHelper.RPop(key);
            if (string.IsNullOrEmpty(value))
                return null;
            return value;
        }

        /// <summary>
        /// 移除list中的数据，keepIndex为保留的位置到最后一个元素如list 元素为1.2.3.....100
        /// 需要移除前3个数，keepindex应该为4
        /// </summary>
        /// <param name="key"></param>
        /// <param name="keepIndex"></param>
        public void ListRemove(string key, CacheDataType dataType, int keepIndex)
        {
            RedisHelper.LTrim(key, keepIndex, -1);
        }
        public bool Add(string key, CacheDataType dataType, string value, int expireSeconds = -1, bool isSliding = false)
        {
            key = this.RedisKey(key, dataType);
            return RedisHelper.Set(key, value, expireSeconds);
        }
        public bool AddObject(string key, CacheDataType dataType, object value,int expireSeconds = -1, bool isSliding = false)
        {
            key = this.RedisKey(key, dataType);
            return RedisHelper.Set(key, value, expireSeconds);
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public bool Remove(string key, CacheDataType dataType)
        {
            key = this.RedisKey(key, dataType);
            return RedisHelper.Del(key)>0;
        }
        /// <summary>
        /// 批量删除缓存
        /// </summary>
        /// <param name="key">缓存Key集合</param>
        /// <returns></returns>
        public void RemoveAll(List<string> keys, CacheDataType dataType)
        {
            List<string> keysnew = new List<string>();
            foreach (var item in keys)
            {
                keysnew.Add( this.RedisKey(item, dataType));
            }
            RedisHelper.Del(keysnew.ToArray());
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public T Get<T>(string key, CacheDataType dataType) where T : class
        {
            return JsonConvert.DeserializeObject<T>(this.Get(key, dataType));
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public string Get(string key, CacheDataType dataType)
        {
            key= this.RedisKey(key, dataType);
            return RedisHelper.Get(key);
        }
        
        public void Dispose()
        {
        }

        /// <summary>
        /// 拼接名字
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dataType"></param>
        /// <returns></returns>
        private string RedisKey(string key, CacheDataType dataType)
        {
            string KeyName = "";
            switch (dataType)
            {
                case CacheDataType.UserInfo:
                    KeyName = key+ ":UserInfo";
                    break;
                case CacheDataType.UserRole:
                    KeyName = key + ":UserRole";
                    break;
                case CacheDataType.UserMenu:
                    KeyName = key + ":UserMenu";
                    break;
                case CacheDataType.VerificationCode:
                    KeyName = "VerificationCode:" + key;
                    break;
                case CacheDataType.DateOther:
                    KeyName = "DateOther:" + key;
                    break;
                default:
                    break;
            }
            return KeyName;
        }

    }
}
